'''
 * @ author     ：廖传港
 * @ date       ：Created in 2020/10/29 10:48
 * @ description：该文件是对图片做预处理，主要有调整大小（resize_image()函数）和灰度化
 * @ modified By：
 * @ ersion     : 
 * @File        : Image_preprocessing.py 
'''
#该文件是对图片做预处理，主要有resize_image()函数和灰度化
import os
import cv2
import numpy as np

IMAGE_SIZE = 64

def resize_image(image, height=IMAGE_SIZE, width=IMAGE_SIZE):
    top, bottom, left, right = (0, 0, 0, 0)

    h, w, _= image.shape

    shortest_edge = max(h, w)
    if h > shortest_edge:
        dh = h - shortest_edge
        top = dh // 2
        bottom = dh - top
    elif w > shortest_edge:
        dw = w - shortest_edge
        left = dw // 2
        righ = dw - left
    else:
        pass

    BLACK = [0, 0, 0]

    constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)

    return cv2.resize(constant, (height, width))


if __name__ == '__main__':
    path_name1 = "D:/python/data/data5/cat"#这个为你想要预处理文件的路径
    path_name2 = "D:/python/data/data5/dog"#这个为你想要预处理文件的路径

    i = 0
    for dir_item in os.listdir(path_name1):
        full_path = os.path.abspath(os.path.join(path_name1, dir_item))
        i += 1
        image = cv2.imread(full_path)       #读取出照片
        image = resize_image(image)         #将图片大小转为64*64
        # image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)     #将图片转为灰度图
        cv2.imwrite(full_path,image)
    for dir_item in os.listdir(path_name2):
        full_path = os.path.abspath(os.path.join(path_name2, dir_item))
        i += 1
        image = cv2.imread(full_path)       #读取出照片
        image = resize_image(image)         #将图片大小转为64*64
        # image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)     #将图片转为灰度图
        cv2.imwrite(full_path,image)
